# -*- coding: utf-8 -*-
__all__ = ['base_path', 'g_rint', 'env', 'session_store', 'store',
           'sys_logger','apperr_logger','event_logger','thrown_logger','cache']

# 调试开关
import os
#debug = False 
debug = True

# 获取当前目录
from os.path import abspath, dirname, join
base_path = abspath(dirname(__file__))

# 全站资源随机数
import random
import stat
import time
final_time = 0
js_dir = os.path.join(base_path,'static','js')
for f in os.listdir(js_dir):
    tmp = os.stat(os.path.join(js_dir,f))[stat.ST_MTIME]
    if final_time < tmp:
        final_time = tmp
g_rint = final_time

# 添加系统路径
import sys
sys.path.insert(0, abspath(join(base_path, '..', 'lib')))

from werkzeug.contrib.cache import MemcachedCache, NullCache, SimpleCache, FileSystemCache
cache = MemcachedCache(['cacheServer:11211'], key_prefix="vnet.")
cache.clear()

bytecode_path = join(base_path, 'static', 'bytecode')
if not os.path.exists(bytecode_path): os.makedirs(bytecode_path)
# 设置模板目录
from accost.utils.tmpl import FragmentCacheExtension
from jinja2 import Environment, PackageLoader, MemcachedBytecodeCache, FileSystemBytecodeCache
env = Environment(
    loader = PackageLoader('chee', 'templates'),
    auto_reload = debug,
    extensions = [FragmentCacheExtension],
    bytecode_cache = FileSystemBytecodeCache(bytecode_path)
)
env.fragment_cache = cache

# 设置 session
from accost.sessions import SessionStore
session_store = SessionStore()

# 配置数据库
db = {}
from accost.utils.db import ThreadSafeStore
if os.name == 'posix' and os.uname()[1]!='python':
    store = db['store'] = ThreadSafeStore('mysql://video:chee@videodb:3306/plant')
else:
    store = db['store'] = ThreadSafeStore('mysql://root:123@localhost:3306/plant')

# 日志
import logging
from logging.handlers import TimedRotatingFileHandler
# check if log directory exists
sys_log_path = join(base_path, 'static', 'log', 'sys')
apperr_log_path = join(base_path, 'static', 'log', 'app')
event_log_path = join(base_path, 'static', 'log', 'event')
thrown_log_path = join(base_path, 'static', 'log', 'vsdn')
if not os.path.exists(sys_log_path): os.makedirs(sys_log_path)
if not os.path.exists(apperr_log_path): os.makedirs(apperr_log_path)
if not os.path.exists(event_log_path): os.makedirs(event_log_path)
if not os.path.exists(thrown_log_path): os.makedirs(thrown_log_path)

syslog = TimedRotatingFileHandler(join(sys_log_path, 'system.log'), 'MIDNIGHT')
syslog.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
syslog.setLevel(logging.NOTSET)
sys_logger = logging.getLogger('system')
sys_logger.addHandler(syslog)

apperrlog = TimedRotatingFileHandler(join(apperr_log_path, 'app.log'), 'MIDNIGHT')
apperrlog.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
apperrlog.setLevel(logging.NOTSET)
apperr_logger = logging.getLogger('apperr')
apperr_logger.addHandler(apperrlog)

evtlog = TimedRotatingFileHandler(join(event_log_path, 'event.log'), 'MIDNIGHT')
evtlog.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
evtlog.setLevel(logging.NOTSET)
event_logger = logging.getLogger('event')
event_logger.addHandler(evtlog)

thrownlog = TimedRotatingFileHandler(join(thrown_log_path, 'vsdn.log'), 'MIDNIGHT')
thrownlog.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
thrownlog.setLevel(logging.NOTSET)
thrown_logger = logging.getLogger('vsdn')
thrown_logger.addHandler(thrownlog)

def getSysLogger(): return sys_logger
def getAppErrLogger(): return apperr_logger
def getEventLogger(): return event_logger
def getThrownLogger(): return thrown_logger

